package structuralModel.combinationModel.organization;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

public class client {

	public static void main(String[] args) {

		List<PhysicalBoundary> physicalBoundaries = JSONArray.parseArray(getResultStr(), PhysicalBoundary.class);

		List<PhysicalBoundary> physicalBoundaryList = physicalBoundaries.stream()
				.sorted(Comparator.comparing(PhysicalBoundary::getParentCode))
				.collect(Collectors.toList());

		// 父级节点的Map 及 子级节点 List<PhysicalBoundary>
		Map<String, List<PhysicalBoundary>> parentCodePhysicalBoundaryMap
				= physicalBoundaryList.stream().collect(Collectors.groupingBy(PhysicalBoundary::getParentCode));

		// 根节点
		PhysicalBoundaryRootCompoent rootCompoent = new PhysicalBoundaryRootCompoent(physicalBoundaryList.get(0));
		// 避免重复处理 Set
		Set<String> visitedOrgCodeSet = new HashSet<>();
		visitedOrgCodeSet.add(physicalBoundaryList.get(0).getOrgCode());
		for (PhysicalBoundary boundary : physicalBoundaryList) {
			if (visitedOrgCodeSet.contains(boundary.getOrgCode())){
				continue;
			}
			PhysicalBoundaryCompoent node = getNodeFrom(rootCompoent, boundary.getParentCode());
			// 从Map 中获取 组织边界列表信息
			List<PhysicalBoundary> boundaryList = parentCodePhysicalBoundaryMap.get(boundary.getParentCode());
			// 如果为空, 说明没有字节点了, 那就是一个叶子节点
			if (CollectionUtils.isEmpty(boundaryList)){
				PhysicalBoundaryLeafCompoent physicalBoundaryLeafCompoent = new PhysicalBoundaryLeafCompoent(boundary);
				node.addElement(physicalBoundaryLeafCompoent);
				continue;
			}
			// 不为空, 说明有字节点, 那就是一个层级节点
			for (PhysicalBoundary physicalBoundary : boundaryList) {
				node.addElement(new PhysicalBoundaryLevelCompoent(physicalBoundary));
				visitedOrgCodeSet.add(physicalBoundary.getOrgCode());
			}
		}
		System.out.println(JSON.toJSONString(rootCompoent));
		List<PhysicalBoundaryDto> result = bsfGenerateResult(rootCompoent);
		rootCompoent.queryAll();
	}

	/**
	 * bsf生成结果
	 *
	 * @param rootCompoent 根成分
	 * @return {@code List<PhysicalBoundaryDto> }
	 * @author ZhangYi
	 */
	private static List<PhysicalBoundaryDto> bsfGenerateResult(PhysicalBoundaryRootCompoent rootCompoent) {
		if (rootCompoent == null
				|| rootCompoent.getPhysicalBoundary() == null
				|| CollectionUtils.isEmpty(rootCompoent.getPhysicalBoundaryCompoentList())) {
			return new ArrayList<>();
		}
		List<PhysicalBoundaryDto> result = new ArrayList<>();
		// 宽度优先遍历队列
		Queue<PhysicalBoundaryCompoent> queue = new LinkedList<>();
		queue.offer(rootCompoent);

		// 父级的orgCode 和 字节点 list Map 映射
		Map<String,List<PhysicalBoundaryDto>> orgCodeChildreListMap = new HashMap<>();
		PhysicalBoundaryDto boundaryDto = convert2PhysicalBoundaryDto(rootCompoent);
		orgCodeChildreListMap.put(boundaryDto.getOrgCode(),boundaryDto.getPhysicalBoundaryList());

		result.add(boundaryDto);

		while (!queue.isEmpty()) {
			PhysicalBoundaryCompoent compoent = queue.poll();
			List<PhysicalBoundaryCompoent> compoentList = compoent.getPhysicalBoundaryCompoentList();
			if (CollectionUtils.isEmpty(compoentList)){
				continue;
			}
			for (PhysicalBoundaryCompoent boundaryCompoent : compoentList) {
				List<PhysicalBoundaryDto> dtos = orgCodeChildreListMap.get(compoent.getPhysicalBoundary().getOrgCode());
				queue.offer(boundaryCompoent);
				PhysicalBoundaryDto dto = convert2PhysicalBoundaryDto(boundaryCompoent);
				dtos.add(dto);
				orgCodeChildreListMap.put(dto.getOrgCode(),dto.getPhysicalBoundaryList());
			}
		}
		return result;
	}

	/**
	 * 转换2物理边界dto
	 *
	 * @param physicalBoundaryCompoent 物理边界组件
	 * @return {@code PhysicalBoundaryDto }
	 * @author ZhangYi
	 */
	private static PhysicalBoundaryDto convert2PhysicalBoundaryDto(PhysicalBoundaryCompoent physicalBoundaryCompoent) {
		PhysicalBoundaryDto boundaryDto = new PhysicalBoundaryDto();
		PhysicalBoundary physicalBoundary = physicalBoundaryCompoent.getPhysicalBoundary();
		boundaryDto.setId(physicalBoundary.getId());
		boundaryDto.setOrgCode(physicalBoundary.getOrgCode());
		boundaryDto.setOrgName(physicalBoundary.getOrgName());
		boundaryDto.setOrgLevel(physicalBoundary.getOrgLevel());
		boundaryDto.setParentCode(physicalBoundary.getParentCode());
		boundaryDto.setProvinceCode(physicalBoundary.getProvinceCode());
		boundaryDto.setHolderIdList(physicalBoundary.getHolderIdList());
		boundaryDto.setOrgPath(physicalBoundary.getOrgPath());
		boundaryDto.setOrgPathName(physicalBoundary.getOrgPathName());
		boundaryDto.setEmail(physicalBoundary.getEmail());
		return boundaryDto;
	}


	/**
	 * 从获取节点
	 *
	 * @param root       根
	 * @param parentCode 父级代码
	 * @return {@code PhysicalBoundaryCompoent }
	 * @author ZhangYi
	 */
	private static PhysicalBoundaryCompoent getNodeFrom(PhysicalBoundaryCompoent root, String parentCode) {
		if (root.getPhysicalBoundary().getOrgCode().equals(parentCode)){
			return root;
		}

		PhysicalBoundaryCompoent compoent = null;
		for (PhysicalBoundaryCompoent physicalBoundaryCompoent : root.getPhysicalBoundaryCompoentList()) {
			compoent = getNodeFrom( physicalBoundaryCompoent,parentCode);
			if (compoent != null){
				return compoent;
			}
		}
		return compoent;
	}


	/**
	 * 获取结果str
	 *
	 * @return {@code String }
	 * @author ZhangYi
	 */
	private static String getResultStr(){
		return "[\n" +
				"  {\n" +
				"    \"id\": 209,\n" +
				"    \"org_code\": \"100000\",\n" +
				"    \"org_name\": \"中国外运股份有限公司\",\n" +
				"    \"org_level\": \"2\",\n" +
				"    \"parent_code\": \"000000\",\n" +
				"    \"org_path\": \"/000000/100000\",\n" +
				"    \"org_path_name\": \"中国外运股份有限公司\",\n" +
				"    \"province_code\": null,\n" +
				"    \"create_time\": \"2024-06-14 16:18:20.559998\",\n" +
				"    \"update_time\": null,\n" +
				"    \"create_user\": \"1\",\n" +
				"    \"update_user\": null,\n" +
				"    \"deleted\": false\n" +
				"  },\n" +
				"  {\n" +
				"    \"id\": 210,\n" +
				"    \"org_code\": \"100605\",\n" +
				"    \"org_name\": \"中外运空运有限公司\",\n" +
				"    \"org_level\": \"3\",\n" +
				"    \"parent_code\": \"100000\",\n" +
				"    \"org_path\": \"/000000/100000/100605\",\n" +
				"    \"org_path_name\": \"中外运空运有限公司/中国外运股份有限公司\",\n" +
				"    \"province_code\": null,\n" +
				"    \"create_time\": \"2024-06-14 16:18:20.560194\",\n" +
				"    \"update_time\": null,\n" +
				"    \"create_user\": \"1\",\n" +
				"    \"update_user\": null,\n" +
				"    \"deleted\": false\n" +
				"  },\n" +
				"  {\n" +
				"    \"id\": 211,\n" +
				"    \"org_code\": \"100160\",\n" +
				"    \"org_name\": \"中外运物流有限公司\",\n" +
				"    \"org_level\": \"3\",\n" +
				"    \"parent_code\": \"100000\",\n" +
				"    \"org_path\": \"/000000/100000/100160\",\n" +
				"    \"org_path_name\": \"中外运物流有限公司/中国外运股份有限公司\",\n" +
				"    \"province_code\": null,\n" +
				"    \"create_time\": \"2024-06-14 16:18:20.560369\",\n" +
				"    \"update_time\": null,\n" +
				"    \"create_user\": \"1\",\n" +
				"    \"update_user\": null,\n" +
				"    \"deleted\": false\n" +
				"  },\n" +
				"  {\n" +
				"    \"id\": 212,\n" +
				"    \"org_code\": \"101441\",\n" +
				"    \"org_name\": \"SE Logistics Holding B.V.\",\n" +
				"    \"org_level\": \"3\",\n" +
				"    \"parent_code\": \"100000\",\n" +
				"    \"org_path\": \"/000000/100000/101441\",\n" +
				"    \"org_path_name\": \"SE Logistics Holding B.V./中国外运股份有限公司\",\n" +
				"    \"province_code\": null,\n" +
				"    \"create_time\": \"2024-06-14 16:18:20.560595\",\n" +
				"    \"update_time\": null,\n" +
				"    \"create_user\": \"1\",\n" +
				"    \"update_user\": null,\n" +
				"    \"deleted\": false\n" +
				"  },\n" +
				"  {\n" +
				"    \"id\": 213,\n" +
				"    \"org_code\": \"100749\",\n" +
				"    \"org_name\": \"中外运空运日本株式会社\",\n" +
				"    \"org_level\": \"4\",\n" +
				"    \"parent_code\": \"100605\",\n" +
				"    \"org_path\": \"/000000/100000/100605/100749\",\n" +
				"    \"org_path_name\": \"中外运空运日本株式会社/中外运空运有限公司/中国外运股份有限公司\",\n" +
				"    \"province_code\": null,\n" +
				"    \"create_time\": \"2024-06-14 16:18:20.560784\",\n" +
				"    \"update_time\": null,\n" +
				"    \"create_user\": \"1\",\n" +
				"    \"update_user\": null,\n" +
				"    \"deleted\": false\n" +
				"  },\n" +
				"  {\n" +
				"    \"id\": 214,\n" +
				"    \"org_code\": \"101522\",\n" +
				"    \"org_name\": \"中外运物流有限公司西北区域\",\n" +
				"    \"org_level\": \"4\",\n" +
				"    \"parent_code\": \"100160\",\n" +
				"    \"org_path\": \"/000000/100000/100160/101522\",\n" +
				"    \"org_path_name\": \"中外运物流有限公司西北区域/中外运物流有限公司/中国外运股份有限公司\",\n" +
				"    \"province_code\": null,\n" +
				"    \"create_time\": \"2024-06-14 16:18:20.560920\",\n" +
				"    \"update_time\": null,\n" +
				"    \"create_user\": \"1\",\n" +
				"    \"update_user\": null,\n" +
				"    \"deleted\": false\n" +
				"  },\n" +
				"  {\n" +
				"    \"id\": 215,\n" +
				"    \"org_code\": \"101349\",\n" +
				"    \"org_name\": \"中外运空运有限公司长沙分公司\",\n" +
				"    \"org_level\": \"4\",\n" +
				"    \"parent_code\": \"100605\",\n" +
				"    \"org_path\": \"/000000/100000/100605/101349\",\n" +
				"    \"org_path_name\": \"中外运空运有限公司长沙分公司/中外运空运有限公司/中国外运股份有限公司\",\n" +
				"    \"province_code\": null,\n" +
				"    \"create_time\": \"2024-06-14 16:18:20.561063\",\n" +
				"    \"update_time\": null,\n" +
				"    \"create_user\": \"1\",\n" +
				"    \"update_user\": null,\n" +
				"    \"deleted\": false\n" +
				"  },\n" +
				"  {\n" +
				"    \"id\": 216,\n" +
				"    \"org_code\": \"101328\",\n" +
				"    \"org_name\": \"中外运空运有限公司北京分公司\",\n" +
				"    \"org_level\": \"4\",\n" +
				"    \"parent_code\": \"100605\",\n" +
				"    \"org_path\": \"/000000/100000/100605/101328\",\n" +
				"    \"org_path_name\": \"中外运空运有限公司北京分公司/中外运空运有限公司/中国外运股份有限公司\",\n" +
				"    \"province_code\": null,\n" +
				"    \"create_time\": \"2024-06-14 16:18:20.561236\",\n" +
				"    \"update_time\": null,\n" +
				"    \"create_user\": \"1\",\n" +
				"    \"update_user\": null,\n" +
				"    \"deleted\": false\n" +
				"  },\n" +
				"  {\n" +
				"    \"id\": 217,\n" +
				"    \"org_code\": \"101192\",\n" +
				"    \"org_name\": \"中外运物流西北有限公司\",\n" +
				"    \"org_level\": \"5\",\n" +
				"    \"parent_code\": \"101522\",\n" +
				"    \"org_path\": \"/000000/100000/100160/101522/101192\",\n" +
				"    \"org_path_name\": \"中外运物流西北有限公司/中外运物流有限公司西北区域/中外运物流有限公司/中国外运股份有限公司\",\n" +
				"    \"province_code\": null,\n" +
				"    \"create_time\": \"2024-06-14 16:18:20.561382\",\n" +
				"    \"update_time\": null,\n" +
				"    \"create_user\": \"1\",\n" +
				"    \"update_user\": null,\n" +
				"    \"deleted\": false\n" +
				"  },\n" +
				"  {\n" +
				"    \"id\": 218,\n" +
				"    \"org_code\": \"100618\",\n" +
				"    \"org_name\": \"北京中外运嘉航物流有限公司\",\n" +
				"    \"org_level\": \"5\",\n" +
				"    \"parent_code\": \"101328\",\n" +
				"    \"org_path\": \"/000000/100000/100605/101328/100618\",\n" +
				"    \"org_path_name\": \"北京中外运嘉航物流有限公司/中外运空运有限公司北京分公司/中外运空运有限公司/中国外运股份有限公司\",\n" +
				"    \"province_code\": null,\n" +
				"    \"create_time\": \"2024-06-14 16:18:20.561518\",\n" +
				"    \"update_time\": null,\n" +
				"    \"create_user\": \"1\",\n" +
				"    \"update_user\": null,\n" +
				"    \"deleted\": false\n" +
				"  }\n" +
				"]";
	}
}
